using System;
using System.IO;
using System.Windows.Forms;

namespace IPAMS.Views
{
    /// <summary>
    /// This is a user control that enables to set up the portfolio definitions and data.
    /// </summary>
    public partial class PortfolioSetup : UserControl
    {
        
        //the EventHandler<TEventArgs<DateTime>> is the type of the handling method
        public event EventHandler<TEventArgs<DateTime>> StartDateChanged;
        public event EventHandler<TEventArgs<DateTime>> EndDateChanged;
        public event EventHandler<TEventArgs<string>> InvestmentHouseChanged;

        private string _dataFilePath =  Application.StartupPath + @"\DummyData\InvestmentHouses.csv";
        private DSIPAMS.DTInvestmentManagementHousesDataTable investmentHouses;

        //headers are :
        //Investment House Code : Investment House Name  : Investment House Country Code

        /// <summary>
        /// Initializes a new instance of the <see cref="PortfolioSetup"/> class.
        /// </summary>
        public PortfolioSetup()
        {
            InitializeComponent();
            textBoxSelectedPath.Text = Application.StartupPath + @"\DummyData";
            investmentHouses = new DSIPAMS.DTInvestmentManagementHousesDataTable();
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the comboBoxStockExchangeHouses control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void comboBoxStockExchangeHouses_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(comboBoxInvestmentHouses.Text != string.Empty)
            {
                lblIHSymbol.Text =
                    investmentHouses.Select(string.Format("INVESTMENT_HOUSE_NAME = '{0}'", comboBoxInvestmentHouses.Text))[0]["INVESTMENT_HOUSE_CODE"].ToString();
                lblIHCountry.Text =
                     investmentHouses.Select(string.Format("INVESTMENT_HOUSE_NAME = '{0}'", comboBoxInvestmentHouses.Text))[0]["INVESTMENT_HOUSE_COUNTRY_CODE"].ToString();



                OnInvestmentHouseChanged(this,
                                         new TEventArgs<string>(
                                             investmentHouses.Select(
                                                 string.Format("INVESTMENT_HOUSE_NAME = '{0}'",
                                                               comboBoxInvestmentHouses.Text))[0][
                                                 "INVESTMENT_HOUSE_CODE"].ToString()));

            }
        }

        /// <summary>
        /// Loads the data from a file.
        /// </summary>
        /// <param name="dataFilePath">The data file path.</param>
        public void LoadData(string dataFilePath)
        {

            if (File.Exists(dataFilePath))
            {
                StreamReader sr = new StreamReader(dataFilePath);
                string[] headers = sr.ReadLine().Split(','); //read the headers
                while (true)
                {
                    string stockExchange = sr.ReadLine();
                    if (stockExchange == string.Empty || stockExchange == null) break;
                    string[] stockExchangeInfo = stockExchange.Split(',');
                    if (stockExchangeInfo.Length != 3) continue;
                    investmentHouses.AddDTInvestmentManagementHousesRow(stockExchangeInfo[0], stockExchangeInfo[1], stockExchangeInfo[2]);
                    comboBoxInvestmentHouses.Items.Add(stockExchangeInfo[1]);
                }
                sr.Close();
                Text = dataFilePath;

                investmentHouses.AcceptChanges();

                

            }
            return;
        }


        /// <summary>
        /// Initializes this instance.
        /// </summary>
        internal void Initialize()
        {
            LoadData(_dataFilePath);
        }


        /// <summary>
        /// Called when [start date changed].
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="newStartTime">The <see cref="IPAMS.TEventArgs&lt;System.DateTime&gt;"/> instance containing the event data.</param>
        protected virtual void OnStartDateChanged(object obj, TEventArgs<DateTime> newStartTime)
        {
            if(StartDateChanged != null)
            {
                StartDateChanged(obj, newStartTime);
            }
        }

        /// <summary>
        /// Called when [end date changed].
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="newEndTime">The <see cref="IPAMS.TEventArgs&lt;System.DateTime&gt;"/> instance containing the event data.</param>
        protected virtual void OnEndDateChanged(object obj, TEventArgs<DateTime> newEndTime)
        {
            if (EndDateChanged != null)
            {
                EndDateChanged(obj, newEndTime);
            }
        }

        /// <summary>
        /// Called when [investment house changed].
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="newInvestmentHouse">The <see cref="IPAMS.TEventArgs&lt;System.String&gt;"/> instance containing the event data.</param>
        protected virtual void OnInvestmentHouseChanged(object obj, TEventArgs<string > newInvestmentHouse)
        {
            if (InvestmentHouseChanged != null)
            {
                InvestmentHouseChanged(obj, newInvestmentHouse);
            }
        }

        /// <summary>
        /// Handles the DateChanged event of the monthCalendarStart control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DateRangeEventArgs"/> instance containing the event data.</param>
        private void monthCalendarStart_DateChanged(object sender, DateRangeEventArgs e)
        {
            OnStartDateChanged(this, new TEventArgs<DateTime>(monthCalendarStart.SelectionStart));
        }

        /// <summary>
        /// Handles the DateChanged event of the monthCalendarEnd control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DateRangeEventArgs"/> instance containing the event data.</param>
        private void monthCalendarEnd_DateChanged(object sender, DateRangeEventArgs e)
        {
            OnEndDateChanged(this,new TEventArgs<DateTime>(monthCalendarEnd.SelectionStart));
        }

        /// <summary>
        /// Handles the Click event of the buttonBrowse control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog selectFolder = new FolderBrowserDialog();
            selectFolder.Description = "Select the folder with the data files.";
            selectFolder.ShowNewFolderButton = true;
            if(selectFolder.ShowDialog()== DialogResult.OK)
            {
                textBoxSelectedPath.Text = selectFolder.SelectedPath;
            }
        }
    }
}
